package chuichui.game
{

	import chuichui.Utils.Print;
	import chuichui.game.components.GameComponent;
	import chuichui.game.update.Update;
	import chuichui.interfaces.IUpdate;
	import chuichui.manager.GameManager;

	import flash.utils.Dictionary;


	public class GameBase extends Update
	{

		public var id : int;
		private var comIndex : int = 0;
		private var gameComps : Array = [];
		private var frameUpdatables : Array = [];
		private var typeComponent : Dictionary = new Dictionary(false);

		public function GameBase(id : int)
		{
			this.id = id;
			super();
		}

		/**
		 * 为对象添加组件
		 * @param com
		 * @param priority
		 *
		 */
		public function addComponent(comp : GameComponent, priority : int = 0) : void
		{
			if(!isActive)
				return;
			var index : int = gameComps.indexOf(comp);

			if(index != -1)
			{
				Print.error("重复添加组件:", comp, comp.type);
				return;
			}
			gameComps.push(comp);
			comp.index = comIndex++;

			if(comp is IUpdate)
			{
				(comp as IUpdate).priority = priority;
				frameUpdatables.push(comp);

				if(priority != 0)
					frameUpdatables.sortOn(['priority', 'index'], [Array.NUMERIC, Array.DESCENDING | Array.DESCENDING, Array.NUMERIC]);
			}
			typeComponent[comp.type] = comp;
			comp.owner = this;
			comp.notifyAdded();
		}

		public function removeComponentByType(type : *) : void
		{
			removeComponent(getComponent(type));
		}

		public function removeComponent(comp : GameComponent) : void
		{
			if(!isActive)
			{
				return;
			}

			if(comp && typeComponent[comp.type] == comp && comp.owner == this)
			{
				var index : int = gameComps.indexOf(comp);

				if(index != -1)
				{
					gameComps.splice(index, 1);
				}
				index = frameUpdatables.indexOf(comp);

				if(index != -1)
				{
					frameUpdatables.splice(index, 1);
				}
				delete typeComponent[comp.type];
				comp.owner = null;

				comp.notifyRemoved(this);
			}
		}

		public function getComponent(type : *) : *
		{
			return typeComponent == null ? null : typeComponent[type];
		}

		private function notifyResetComponent() : void
		{
			for each(var comp : GameComponent in gameComps)
			{
				comp.notifyReset();
			}
		}

		override public function update(elapsedTime : uint) : void
		{
			if(isDestroy)
			{
				return;
			}
			var len : int = frameUpdatables.length;

			for(var i : int = len - 1; i >= 0; i -= 1)
			{
				if(isDestroy)
				{
					return;
				}
				var updatable : IUpdate = frameUpdatables[i];

				if(updatable && updatable.checkUpdatable(elapsedTime))
				{
					updatable.update(elapsedTime);
				}
			}
		}

		public function get isActive() : Boolean
		{
			return !isDestroy;
		}

		/**
		 *
		 * @param level   更新的等级,0低级,1中级,2高级
		 * @param priority优先级别
		 *
		 */
		override public function registerd(registerdLevel : int = LOW, priority : int = 0) : void
		{
			if(GameManager.getInstance().addUpdate(this, registerdLevel, priority))
			{
				this.registerdLevel = registerdLevel;
			}
		}

		override public function unRegisterd() : void
		{
			GameManager.getInstance().removeUpdate(this, registerdLevel);
		}

		override public function destroy() : void
		{
			if(!isActive)
				return;
			unRegisterd();
			var len : int = gameComps.length;

			for(var i : int = len - 1; i >= 0; i--)
			{
				var comp : GameComponent = gameComps[i];

				if(comp)
				{
					comp.destroy();
				}
			}
			_isDestroy = true;
			gameComps.length = 0;
			frameUpdatables.length = 0;
			gameComps = null;
			frameUpdatables = null;
			typeComponent = null;
		}
	}
}
